package com.yeyv.net.Web.Control.Anonymous;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeyv.net.Annotation.ControllerApi.AnonController;
import com.yeyv.net.Web.Entity.GameAuthEntity;
import com.yeyv.net.Web.Service.GameAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@AnonController("/gameAuth")
public class GameAuthAnonController {
    @Autowired
    private GameAuthService gameAuthService;

    @PostMapping("/sendAuth")
    public String sendAuth(@RequestBody GameAuthEntity authEntity) {
        var auth = gameAuthService.getOne(
                new LambdaQueryWrapper<GameAuthEntity>()
                        .eq(GameAuthEntity::getGame, authEntity.getGame())
                        .eq(GameAuthEntity::getAccount, authEntity.getAccount()
                        )
        );
        if (auth != null) {
            List<String> changeProp = new ArrayList<>();
            if (!Objects.equals(auth.getPassword(), authEntity.getPassword())) {
                auth.setPassword(authEntity.getPassword());
                changeProp.add("授权码");
            }
            if (!Objects.equals(auth.getMail(), authEntity.getMail())) {
                auth.setMail(authEntity.getMail());
                changeProp.add("邮箱");
            }
            if (!Objects.equals(auth.getQu(), authEntity.getQu())) {
                auth.setQu(authEntity.getQu());
                changeProp.add("区服");
            }
            auth.setAuthed(1);
            gameAuthService.updateById(auth);
        } else {
            gameAuthService.save(authEntity);
        }
        return null;
    }
}
